package com.flycms.esmodule.schedule;

import com.flycms.esmodule.dao.*;
import com.flycms.esmodule.model.*;
import com.flycms.module.activity.dao.ActivityDao;
import com.flycms.module.activity.model.Activity;
import com.flycms.module.app.dao.AppDao;
import com.flycms.module.app.model.App;
import com.flycms.module.document.dao.DocumentDetailDao;
import com.flycms.module.document.model.Document;
import com.flycms.module.document.model.DocumentDetail;
import com.flycms.module.soft.dao.CaseDao;
import com.flycms.module.soft.dao.SoftDao;
import com.flycms.module.solution.dao.SolutionDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author huangzt
 * @version 1.0
 * @date 2020/3/15 19:49
 */
@Component
public class EsIndexSchedule {

    protected Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    SoftDao softDao;
    @Autowired
    SolutionDao solutionDao;
    @Autowired
    CaseDao caseDao;
    @Autowired
    ActivityDao activityDao;
    @Autowired
    AppDao appDao;
    @Autowired
    DocumentDetailDao documentDao;
    @Autowired
    EsSoftBaseDao esSoftBaseDao;
    @Autowired
    EsCaseBaseDao esCaseBaseDao;
    @Autowired
    EsSolutionBaseDao esSolutionBaseDao;
    @Autowired
    EsActivityBaseDao esActivityBaseDao;
    @Autowired
    EsAppBaseDao esAppBaseDao;
    @Autowired
    EsDocumentBaseDao esDocumentBaseDao;

    public void importEsBase() {
        logger.info("【信息】执行定时任务...");
        logger.info("【信息】正在清除数据...");
        try {
            boolean indexSoft = elasticsearchTemplate.indexExists(EsSoftBase.class);
            boolean indexCase = elasticsearchTemplate.indexExists(EsCaseBase.class);
            boolean indexSolution = elasticsearchTemplate.indexExists(EsSolutionBase.class);
            boolean indexActivity = elasticsearchTemplate.indexExists(EsActivityBase.class);
            boolean indexApp = elasticsearchTemplate.indexExists(EsAppBase.class);
            boolean indexDocument = elasticsearchTemplate.indexExists(EsDocumentBase.class);
            logger.info("【信息】数据清除完毕，正在导入数据...");
            if (indexSoft) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsSoftBase.class);
                if (delete) {
                    putSoft();
                }
            } else {
                putSoft();
            }
            if (indexCase) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsCaseBase.class);
                if (delete) {
                    putCase();
                }
            } else {
                putCase();
            }
            if (indexSolution) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsSolutionBase.class);
                if (delete) {
                    putSolution();
                }
            } else {
                putSolution();
            }
            if (indexActivity) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsActivityBase.class);
                if (delete) {
                    putActivity();
                }
            } else {
                putActivity();
            }
            if (indexApp) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsAppBase.class);
                if (delete) {
                    putApp();
                }
            } else {
                putApp();
            }
            if (indexDocument) {
                boolean delete = elasticsearchTemplate.deleteIndex(EsDocumentBase.class);
                if (delete) {
                    putDocument();
                }
            } else {
                putDocument();
            }

        } catch (Exception e) {
            logger.debug("【error】数据导入失败！error : " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 从mysql查出各类需要全局检索的数据，再往es服务器灌数据
     */

    /**
     * 软件产品
     */
    private void putSoft() {

        List<EsSoft> esSofts = softDao.queryAll();
        if (esSofts != null && esSofts.size() != 0) {
            List<EsSoftBase> esBases = esSofts.stream().map(a -> {
                EsSoftBase esBase = new EsSoftBase();
                esBase.setId(String.valueOf(a.getId()));
                esBase.setCreateTime(a.getCreateTime());
                esBase.setUpdateTime(a.getUpdateTime());
                esBase.setTitle(a.getSoftName());
                esBase.setDescription(a.getSoftDescription() + a.getSoftPlatform() + a.getSoftVersion());
                esBase.setType(1);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsSoftBase> data = esSoftBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】软件数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsSoftBase.class);
        }
    }

    /**
     * 客户案例
     */
    private void putCase() {

        List<EsCase> esCases = caseDao.queryAll();
        if (esCases != null && esCases.size() != 0) {
            List<EsCaseBase> esBases = esCases.stream().map(a -> {
                EsCaseBase esBase = new EsCaseBase();
                esBase.setId(String.valueOf(a.getId()));
                esBase.setCreateTime(a.getCreateTime());
                esBase.setUpdateTime(a.getUpdateTime());
                esBase.setTitle(a.getCaseName());
                esBase.setDescription(a.getCustomDescription());
                esBase.setType(1);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsCaseBase> data = esCaseBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】客户案例数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsCaseBase.class);
        }
    }

    /**
     * 解决方案
     */
    private void putSolution() {

        List<EsSolution> esSolutions = solutionDao.queryAll();
        if (esSolutions != null && esSolutions.size() != 0) {
            List<EsSolutionBase> esBases = esSolutions.stream().map(a -> {
                EsSolutionBase esBase = new EsSolutionBase();
                esBase.setId(String.valueOf(a.getId()));
                esBase.setCreateTime(a.getCreateTime());
                esBase.setUpdateTime(a.getUpdateTime());
                esBase.setTitle(a.getTitle());
                esBase.setDescription(a.getDescription());
                esBase.setType(3);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsSolutionBase> data = esSolutionBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】解决方案数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsSolutionBase.class);
        }
    }

    /**
     * 活动
     */
    private void putActivity() {

        List<Activity> esActivitys = activityDao.queryAll();
        if (esActivitys != null && esActivitys.size() != 0) {
            List<EsActivityBase> esBases = esActivitys.stream().map(a -> {
                EsActivityBase esBase = new EsActivityBase();
                esBase.setId(String.valueOf(a.getId()));
                esBase.setCreateTime(a.getCreatedTime());
                esBase.setUpdateTime(a.getUpdatedTime());
                esBase.setTitle(a.getActivityName());
                esBase.setDescription(a.getDesc());
                esBase.setType(4);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsActivityBase> data = esActivityBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】资讯活动数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsActivityBase.class);
        }
    }

    private void putApp() {
        /**
         * app
         */
        List<App> esApps = appDao.queryAll();
        if (esApps != null && esApps.size() != 0) {
            List<EsAppBase> esBases = esApps.stream().map(a -> {
                EsAppBase esBase = new EsAppBase();
                esBase.setId(String.valueOf(a.getId()));
                esBase.setCreateTime(a.getCreatedTime());
                esBase.setUpdateTime(a.getUpdatedTime());
                esBase.setTitle(a.getAppName());
                esBase.setDescription(a.getDesc());
                esBase.setType(5);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsAppBase> data = esAppBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】工业app数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsAppBase.class);
        }
    }

    /**
     * 帮助文档
     */
    private void putDocument() {

        List<DocumentDetail> esDocuments = documentDao.queryAll();
        if (esDocuments != null && esDocuments.size() != 0) {
            List<EsDocumentBase> esBases = esDocuments.stream().map(a -> {
                EsDocumentBase esBase = new EsDocumentBase();
                esBase.setId(String.valueOf(a.getDocId()));
                esBase.setCreateTime(a.getCreatedTime());
                esBase.setUpdateTime(a.getUpdatedTime());
                esBase.setTitle(a.getDocName());
                esBase.setDescription(a.getPresentation());
                esBase.setType(6);
                return esBase;
            }).collect(Collectors.toList());
            Iterable<EsDocumentBase> data = esDocumentBaseDao.saveAll(esBases);
            if (data != null) {
                logger.info("【success】工业app数据导入完毕！");
            }
        }else{
            elasticsearchTemplate.createIndex(EsDocumentBase.class);
        }
    }
}
